<%@ page language="java" pageEncoding="UTF-8"
	contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
	String url = (String)request.getAttribute("url");
    String body = (String)request.getAttribute("body");
    if(null == url)
    {
        url = "";
    }
    if(null == body)
    {
        body = "";
    }
%>
<html>
	<script type="text/javascript">
		function openRequest()
		{
			// alert("openRequest coming");
			document.autoForm.action="<%=request.getContextPath()%>/openRequest.html";
			document.autoForm.submit();
		}
		
		function removeHead()
		{
			document.getElementById("Head").style.display="none";
		}
		
		function plusHead()
		{
			document.getElementById("Head").style.display="block";
		}
		
		function saveFile()
		{
			//alert("save");
			var key = document.getElementById("key").value;
			//alert(key);
			//document.autoForm.enctype = "";
			document.autoForm.action="<%=request.getContextPath()%>/saveRequest.download";
			document.autoForm.submit();			
		}
		
		function executeTest(path) {
			var key = document.getElementById("key").value;
			var value = document.getElementById("value").value;
			if ((key != "" && value == "") || (key == "" && value != "")) {
				alert("key 和 value 必须同时存在");
				return;
			} else {
				document.autoForm.action="<%=request.getContextPath()%>/restClient.html";
				document.autoForm.submit();
			}
		}
		
		function redioChange(th) {
			if (th.value == "GET" || th.value == "DELETE") {
				document.getElementById("body").disabled = true;
				document.getElementById("attachment").disabled = true;
			} else {
				document.getElementById("body").disabled = false;
				document.getElementById("attachment").disabled = false;
			}
		}
	</script>
	<head>
		<title>VIRestClient 测试工具（支持图片、视频）</title>
	</head>

	<body>
		<!-- enctype 设置请求参数的数据格式 (application/x-www-form-urlencoded) 参数名=参数值 
  										mulipart/form-data  数据报
  		-->
		<form action="" method="post" name="autoForm" enctype="multipart/form-data">
			<table align="center" border="1">
				<tr>
					<td colspan="2" align="center" valign="bottom">
						<h3 style="vertical-align: bottom; text-align: center;">
							VIRestClient 测试工具（支持图片、视频）
						</h3>
					</td>
				</tr>
				<tr>
					<td colspan="2" align="center">

					</td>
				</tr>
				<tr>
					<td colspan="2" align="center">
						Http Request
					</td>
				</tr>
				<tr>
					<td>
						Open Request:
					</td>
					<td>
						<input type="file" name="requestFile" id="requestFile" style="width: 500px; height: 25px;"/>
						<input type="button" name="open" onclick="openRequest()" value="open" style="font-size: small; width: 100px; height: 25px;" />
					</td>
				</tr>
				<tr>
					<td style="width: 125px">
						URL：
					</td>
					<td>
						<input type="text" name="url" style="width: 600px"
							value="${url}" />
					</td>
				</tr>
				<tr>
					<td>
						HTTP Method：
					</td>
					<td>
					<%
				    String method = (String)request.getAttribute("method");
					if(method == null)
					{
					    method = "GET";
					}
					String[]  methods = new String[]{"GET", "POST", "PUT", "DELETE"};
					for(String s:methods)
					{
					    if(s.equals(method))
					    {
					        out.print("<input type='radio' name='method'");
					        out.print(" value='");
					        out.print(s);
					        out.print("' checked='checked' onclick='redioChange(this);'/>");
					        out.print(s);
					    }
					    else
					    {
					        out.print("<input type='radio' name='method'");
					        out.print(" value='");
					        out.print(s);
							out.print("' onclick='redioChange(this);'/>");
							out.print(s);
					    }
					}
					%>
						</td>
					</tr>
					<tr>
						<td>
							Headers：
						</td>
						<td>
							Key:
							<input type="text" name="key" id="key" value="${key}"
								style="width: 170px" />
							Value:
							<input type="text" name="value" id="value" value="${value}"
								style="width: 320px" onclick="" />
							<input type="button" name="plus" value="+" style="width: 30px"
								onclick="plusHead()" />
							<%--<input type="button" name="plusplus" value="++"
								style="width: 40px" />
							--%>
							<%
							    String keyTwo = (String)request.getAttribute("keyTwo");
								if(null == keyTwo){
								%>
								<div id="Head" style="display: none">
										Key:
										<input type="text" name="keyTwo" id="keyTwo" value="${keyTwo}"
											style="width: 170px" />
										Value:
										<input type="text" name="valueTwo" id="valueTwo"
											value="${valueTwo}" style="width: 320px" />
										<input type="button" name="remove" value="-" style="width: 30px"
											onclick="removeHead();" />
								</div>
								<% }else{%>
								<div id="Head">
										Key:
										<input type="text" name="keyTwo" id="keyTwo" value="${keyTwo}"
											style="width: 170px" />
										Value:
										<input type="text" name="valueTwo" id="valueTwo"
											value="${valueTwo}" style="width: 320px" />
										<input type="button" name="remove" value="-" style="width: 30px"
											onclick="removeHead();" />
								</div>
							<%} %>
						</td>
					</tr>
					<tr>
						<td>
							Body：
						</td>
						<td>
							<%
							if(method.equals("GET")||method.equals("DELETE"))
							{
							    out.print("<textarea name='body' id='body' style='height: 300px; width: 600px' disabled='disabled'/>");
							    out.print(body);
							    out.print("</textarea>");
							}
							else
							{
							    out.print("<textarea name='body' id='body' style='height: 300px; width: 600px'/>");
							    out.print(body);
							    out.print("</textarea>");
							}
							%>
						</td>
					</tr>
					<tr>
						<td>
							Attachment：
						</td>
						<td>
							<input name="attachment" id="attachment" type="file" style="width: 600px; height: 25px;"/>
						</td>
					</tr>
					<tr>
						<td colspan="2" align="center">
							<input type="button" id="restClient" value="Go"
								style="height: 30px; width: 300px; font-size: large"
								onclick="return executeTest('<%=request.getContextPath()%>');" />
							&nbsp;
							<input type="button" id="save" value="save"
								style="height: 30px; width: 100px; font-size: large"
								onclick="return saveFile();" />
							<%--<input type=button name ="save" style="height: 30px; width: 300px;font-size: large" 
							id="save" value="save" onclick="return saveFile()" />
					--%>
						</td>
					</tr>
			</table>
		</form>

		<br />
		<table align="center" border="1">
			<%
			    if (request.getAttribute("response") != null)
			    {
			%>
			<tr>
				<td colspan="2" align="center" style="width: 730px">
					Http Response
				</td>
			</tr>
			<tr>
				<td style="width: 100px">
					Status：
				</td>
				<td style="width: 500px">
					<%=request.getAttribute("httpStatus")%>
				</td>
			</tr>
			<tr>
				<td style="width: 100px">
					Headers：
				</td>
				<td style="width: 500px">
					<%=request.getAttribute("statusText")%>
				</td>
			</tr>
			<tr>
				<td style="width: 100px">
					Body：
				</td>
				<td style="height: 300px; width: 600px">
					<%=request.getAttribute("response")%>
				</td>
				<td align="center" style="overflow: scroll">
				</td>
			</tr>
			<%
			    }
			%>
		</table>
	</body>
</html>
